package com.gress.shop.controller.authority;

import com.alibaba.fastjson.JSON;
import com.gress.shop.annotation.FireLogger;
import com.gress.shop.bo.Action;
import com.gress.shop.bo.Menu;
import com.gress.shop.enums.MenuEnum;
import com.gress.shop.service.IActionService;
import com.gress.shop.service.IConfigClientService;
import com.gress.shop.service.IMenuService;
import com.gress.shop.util.HappyException;
import com.gress.shop.vo.JsonId;
import com.gress.shop.vo.SwalFactory;
import com.gress.shop.vo.SwalReturn;
import com.wordnik.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xiongwen on 2017/11/27.
 */
@RequestMapping("menu")
@Controller
@Api("后台菜单配置")
public class MenuController {

    @Resource
    private IMenuService menuService;

    @Resource
    private IActionService actionService;

    @Resource
    private IConfigClientService configClientService;

    @RequestMapping("to_menu")
    @FireLogger("布局")
    public String toHtml5(Model model){
        model.addAttribute("sortTree", menuService.buildMenuTree());
        return "menu/menuTree";
    }


    @RequestMapping("ajax_modal_menu_info")
    @FireLogger("详情页面")
    public String ajaxMenuInfo(Model model, String  id) {
        Menu menu = menuService.queryMenuById(id);
        model.addAttribute("menu", menu);
        if (!MenuEnum.MENU.equals(menu.getType())) {
            Action action = actionService.queryActionById(menu.getActionId());
            if (action == null) {
                action = new Action();
                action.setId("");
                action.setActionName("未授权功能");
            }
            model.addAttribute(action);
            model.addAttribute(actionService.queryAllAction());
        }
        return "modal/menu/menuInfo";
    }


    @RequestMapping("ajax_modal_menu_add")
    @FireLogger("新增页面")
    public String ajaxMenuAdd(Model model, String id) {
        Menu menu = menuService.queryMenuById(id);
        model.addAttribute("menu", menu);
        model.addAttribute(actionService.queryAllAction());
        return "modal/menu/addHtml";
    }

    /**
     * @param id
     * @Date: 14:05 2018/2/27
     * @Description: 用于删除菜单的接口
     **/
    @RequestMapping(value = "delete")
    @ResponseBody
    @FireLogger("删除功能")
    public SwalReturn deleteById(String id) {
        return menuService.deleteById(id);
    }


    @RequestMapping(value = "update")
    @ResponseBody
    @FireLogger("更新功能")
    public SwalReturn update(Menu menu) {
        menuService.update(menu);
        return SwalFactory.getSuccessSwal();
    }

    @RequestMapping(value = "save")
    @ResponseBody
    @FireLogger("保存功能或者菜单")
    public SwalReturn save(Menu menu) {
        menuService.save(menu);
        return SwalFactory.getSuccessSwal();
    }

    @RequestMapping(value = "sort_menu")
    @ResponseBody
    @FireLogger("排序")
    public SwalReturn sortMenu(String jsonStr) {
        List<JsonId> jsonIds = JSON.parseArray(jsonStr, JsonId.class);
        if (CollectionUtils.isEmpty(jsonIds)) {
            throw new HappyException("未获取系统信息");
        }
        menuService.sortMenu(jsonIds, "0");
        return SwalFactory.getSuccessSwal();
    }

    @ModelAttribute("functionEnum")
    public MenuEnum[] get(){
        return MenuEnum.getFunctionMenuEnum();
    }

    @ModelAttribute("menus")
    public MenuEnum[] getAllType(){
        return configClientService.queryAllMenuType();
    }
}
